#include <iostream>
#include <sstream>
#include <SFML/Graphics/RenderWindow.hpp>
#include <nice2d/macros.h>
#include <nice2d/scene.h>
#include <nice2d/tilemap.h>
#include <nice2d/background.h>

#define SCENE_WIDTH  960.0f
#define SCENE_HEIGHT 640.0f

class UnitScene : public Scene, public TileVisitor
{
public:
    void onInitialed()override
    {
        setBackground(Background::create("./../data/images/background.png", true));
        setSceneBox({0.0f, 0.0f, SCENE_WIDTH, SCENE_HEIGHT});

        tileMap = TileMap::create(TileMapType::TileMapType_Hex);
        tileMap->init(17, 10, 86.0f);
        tileMap->setTextVisible(true);

        addChild(tileMap);

        tileMap->accept(this);
        tileMap->tileClicked.connect(std::bind(&UnitScene::clickedTile, this, std::placeholders::_1,
                                               std::placeholders::_2));
    }

    void visit(int32_t x, int32_t y, std::shared_ptr<Tile> tile) override
    {
        tile->setFillColor(sf::Color::Transparent);
    }

    void clickedTile(uint32_t x, uint32_t y)
    {
        auto tile = tileMap->getTileByIndex(x, y);
        if (tile)
            tile->setOutlineColor(sf::Color::Blue);
    }
private:
    std::shared_ptr<TileMap> tileMap;
};

DECLARE_MAIN_FUNCTION_WITH_SCENE(SCENE_WIDTH, SCENE_HEIGHT, "unit09", UnitScene)
